package com.liaoban.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.fastjson.JSON;
import com.liaoban.app.R;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.ui.base.BaseActivity;
import com.liaoban.app.util.CommonUtils;
import com.netease.nim.avchatkit.activity.AVChatActivity;
import com.netease.nim.avchatkit.constant.AVChatExtras;
import com.netease.nim.demo.mixpush.DemoMixPushMessageHandler;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.NimIntent;
import com.netease.nimlib.sdk.mixpush.MixPushService;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;

@Deprecated
public class StartActivity extends BaseActivity {
    @Override
    protected int getLayoutResID() {
        //去除title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去掉Activity上面的状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_start;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        Observable.timer(3, TimeUnit.SECONDS).subscribe(aLong -> {
            if(CommonBiz.getInstance().isLogin(StartActivity.this)) {
                // 已经登录过了，处理过来的请求
                Intent intent = getIntent();
                if (intent != null) {
                    if (intent.hasExtra(NimIntent.EXTRA_NOTIFY_CONTENT)) {
                        Log.e("yubch", "parseNotifyIntent()");
                        parseNotifyIntent(intent);
                        return;
                    } else if (NIMClient.getService(MixPushService.class).isFCMIntent(intent)) {
                        Log.e("yubch", "parseFCMNotifyIntent()");
                        parseFCMNotifyIntent(NIMClient.getService(MixPushService.class).parseFCMPayload(intent));
                    } else if (intent.hasExtra(AVChatExtras.EXTRA_FROM_NOTIFICATION) || intent.hasExtra(AVChatActivity.INTENT_ACTION_AVCHAT)) {
                        Log.e("yubch", "parseNormalIntent()");
                        parseNormalIntent(intent);
                    } else {
                        showMainActivity(null);
                        Log.e("yubch", "showMainActivity null");
                    }
                } else {
                    showMainActivity(null);
                    Log.e("yubch", "showMainActivity null");
                }
            } else {
                showMainActivity(null);
            }
        });
    }

    private void parseNotifyIntent(Intent intent) {
        ArrayList<IMMessage> messages = (ArrayList<IMMessage>) intent.getSerializableExtra(NimIntent.EXTRA_NOTIFY_CONTENT);
        if (messages == null || messages.size() > 1) {
            showMainActivity(null);
        } else {
            showMainActivity(new Intent().putExtra(NimIntent.EXTRA_NOTIFY_CONTENT, messages.get(0)));
        }
    }

    private void parseFCMNotifyIntent(String payloadString) {
        Map<String, String> payload = JSON.parseObject(payloadString, Map.class);
        String sessionId = payload.get(DemoMixPushMessageHandler.PAYLOAD_SESSION_ID);
        String type = payload.get(DemoMixPushMessageHandler.PAYLOAD_SESSION_TYPE);
        if (sessionId != null && type != null) {
            int typeValue = Integer.valueOf(type);
            IMMessage message = MessageBuilder.createEmptyMessage(sessionId, SessionTypeEnum.typeOfValue(typeValue), 0);
            showMainActivity(new Intent().putExtra(NimIntent.EXTRA_NOTIFY_CONTENT, message));
        } else {
            showMainActivity(null);
        }
    }

    private void parseNormalIntent(Intent intent) {
        showMainActivity(intent);
    }

    private void showMainActivity(Intent intent) {
        MainActivity.start(StartActivity.this, intent);
        finish();
    }
}
